//
//  GRStoreHeaderView.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/8/24.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRStoreHeaderView.h"
#import "GRPhotoImageCell.h"
#import "GRStoreAnnotation.h"
#import "GRStoreAnnotationView.h"
#import "GRPhotoBrowserVC.h"
@implementation GRStoreHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setPhotoArr:(NSArray *)photoArr{
    _photoArr = photoArr;
    self.topOffset.constant = photoArr.count >0?144:41;
    
    [self.photoList reloadData];
    _mapView.showsScale = NO;
    _mapView.showsCompass = NO;
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    _mapView.delegate = self;
    [_mapView setZoomLevel:15.5 animated:YES];
    [self.mapView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UIImageView class]]) {
            
            UIImageView * logoM = obj;
            
            logoM.layer.contents = (__bridge id)[UIImage imageNamed:@""].CGImage;
        }
    }];
}

-(void)setMenueArr:(NSArray *)menueArr{
    _menueArr = menueArr;
    for (int i = 0; i <menueArr.count; i++) {
        UIButton * button = [self viewWithTag:i+1000];
        [button setTitle:menueArr[i] forState:UIControlStateNormal];
        button.hidden = NO;
    }
}


- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation{
    if ([annotation isKindOfClass:[GRStoreAnnotation  class]]) {
        static NSString *storeAnnotationIdentifier = @"storeAnnotationIdentifier";
        GRStoreAnnotationView * storeAnnotationView =(GRStoreAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:storeAnnotationIdentifier];
        if (!storeAnnotationView) {
            storeAnnotationView = [[ GRStoreAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:storeAnnotationIdentifier];
            storeAnnotationView.canShowCallout   = NO;
            storeAnnotationView.draggable        = YES;
      
        }
        return storeAnnotationView;
    }else{
        static NSString *userAnnotationIdentifier = @"userAnnotationIdentifier";
        GRStoreAnnotationView * storeAnnotationView =(GRStoreAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:userAnnotationIdentifier];
        if (!storeAnnotationView) {
            storeAnnotationView = [[ GRStoreAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:userAnnotationIdentifier];
            storeAnnotationView.canShowCallout = NO;
            storeAnnotationView.draggable        = YES;
        }
        return storeAnnotationView;
        
    }
    
}




-(void)setCoordinate:(CLLocationCoordinate2D)coordinate{
    _coordinate = coordinate;
    GRStoreAnnotation * storeAnnotation = [[GRStoreAnnotation alloc]initWithCoordinate:coordinate];
    storeAnnotation.title = self.storeName;
    [self.mapView addAnnotation:storeAnnotation];
}
-(void)awakeFromNib{
    [super awakeFromNib];
    self.photoList.delegate = self;
    self.photoList.dataSource = self;
    [self.photoList registerNib:[UINib nibWithNibName:@"GRPhotoImageCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"imageCell"];
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GRPhotoImageCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
    [cell.photoImageView sd_setImageWithURL:self.photoArr[indexPath.row]];
    return cell;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.photoArr.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_delegate && [_delegate respondsToSelector:@selector(LookBigPic:PhotoArr:)]) {
        [_delegate LookBigPic:indexPath.row PhotoArr:self.photoArr];
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((KscreenWidth-80)/3, (KscreenWidth-80)/3);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (IBAction)selectedList:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(ScrollerToIndex:)]) {
        [_delegate ScrollerToIndex:sender.tag -1000];
    }
}
- (IBAction)navigationAction:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(openNavi)]) {
        [_delegate openNavi];
    }
}


- (IBAction)callAction:(id)sender {
     [NSObject openSystemURL:[NSString stringWithFormat:@"telprompt://%@",self.moblie]];
}


@end
